/**
 * JFunny - A funny programming language
 * Copyright (C) 2013  fanguangping
 * Any problem email me: fguangping@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 **/
package org.jfunny.core.parser;

import java.io.PushbackReader;

import org.jfunny.core.ds.Symbol;

/**
 * 解析泛语言源代码词法的类
 * 
 * @author fanguangping
 *
 */
public class ScannerFunny {
	public ScannerFunny(PushbackReader reader) {
		this.reader = reader;
	}

	public boolean hasNext() throws Exception {
		while (true) {
			int c = read();

			switch (c) {
			case -1:
				return false;
				/*
				case ';':
					while ('\n' != c && -1 != c)
						c = read();
					break;
				*/
			case '【':
				while ('】' != c && -1 != c)
					c = read();
				break;

			default:
				if (!Character.isWhitespace((char) c)) {
					reader.unread(c);
					return true;
				}
			}
		}
	}

	public Object next() throws Exception {
		while (true) {
			int c = read();

			switch (c) {
			case -1:
				throw new RuntimeException("Unexpected EOS");
			case '\\':
				c = read();
				return new Symbol(readSymbolNameEscaped(c));
			case '{':
				return Token.PAREN_L;
			case '}':
				return Token.PAREN_R;

				//分隔符作为单个符号
			case '、':
				return new Symbol("、");
			case '，':
				return new Symbol("，");
			case '；':
				return new Symbol("；");
				//字符串作为单个符号
			case '"':
				return new Symbol(readString(c));
			default:
				return readSymbol(c);
			}
		}
	}

	private Object readSymbol(int c) throws Exception {
		String symbol = readSymbolName(c);
		return new Symbol(symbol);
	}

	private String readSymbolName(int c) throws Exception {
		buffer.setLength(0);

		while (!Character.isWhitespace((char) c) && '{' != c && '}' != c && '【' != c && '、' != c && '，' != c
				&& '；' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		reader.unread(c);

		return buffer.toString();
	}

	private String readSymbolNameEscaped(int c) throws Exception {
		buffer.setLength(0);

		buffer.append((char) c);
		c = read();
		while (!Character.isWhitespace((char) c) && '{' != c && '}' != c && '【' != c && '、' != c && '，' != c
				&& '；' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		reader.unread(c);

		return buffer.toString();
	}

	private String readString(int c) throws Exception {
		buffer.setLength(0);

		buffer.append((char) c);
		c = read();
		while ('"' != c && -1 != c) {
			if ('\\' == c) {
				c = read();
			}
			buffer.append((char) c);
			c = read();
		}

		buffer.append('"');

		return buffer.toString();
	}

	private final int read() throws Exception {
		int c = reader.read();
		return 65535 == c ? -1 : c;
	}

	private PushbackReader reader;
	private StringBuffer buffer = new StringBuffer();
}
